﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using HAVNet.Core.IO;
using System.Globalization;


namespace GPSServer
{
    public class ServerFiles
    {
        public static void escribirArchivo (string imei, double lat, double lon, double vel, double bea)
        {
            DateTime ahora = DateTime.Now;

            string datos = string.Format ("{0},{1},{2},{3},{4},{5},{6}",
                imei,
                lat.ToString("##0.00000", CultureInfo.InvariantCulture.NumberFormat),
                lon.ToString("##0.00000", CultureInfo.InvariantCulture.NumberFormat),
                vel.ToString("##0.00"   , CultureInfo.InvariantCulture.NumberFormat),
                ((int)bea).ToString(),
                DateTime.Now.ToString("dd/MM/yyyy"),
                DateTime.Now.ToString("HH:mm:ss")
            );

            string path = TreePath.getPath("./", "/", ahora, PathDepth.Day);

            if (! Directory.Exists(path))
                Directory.CreateDirectory(path);

            string fullpath = path + "posiciones";
            bool existeArchivo = File.Exists(fullpath);

            if (! existeArchivo)
                using (StreamWriter sw = File.AppendText(fullpath))
                    sw.WriteLine("imei,lat,lon,vel,bea,dia,hor");

            using (StreamWriter sw = File.AppendText(fullpath))
                sw.WriteLine(datos);
        }
    }
}
